﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Nui.Vision;

namespace kinect_music_shooter.GameWorld
{
    public class Avatar
    {
        public GameObject Head;
        GameObject Neck;
        GameObject LeftShoulder;
        GameObject LeftElbow;
        public GameObject LeftHand;
        GameObject RightShoulder;
        GameObject RightElbow;
        public GameObject RightHand;
        public GameObject Torso;
        GameObject LeftHip;
        GameObject LeftKnee;
        GameObject LeftFoot;
        GameObject RightHip;
        GameObject RightKnee;
        GameObject RightFoot;

        Texture2D pixel_tex;
        float width = 0;
        float height = 0;

        Vector3 OldRightHand = Vector3.Zero;
        Vector3 OldLeftHand = Vector3.Zero;

        Vector3 WurfStart;
        Vector3 WurfEnd;
        Vector3 WurfStartLeft;
        Vector3 WurfEndLeft;

        bool HadokenReady = false;
        bool HadokenActive = false;
        public float HadokenCount = 0;

        bool WurfNullright;
        bool WurfNullleft;
        //Vector3 Head = Vector3.Zero;
        //Vector3 Neck = Vector3.Zero;
        /* Vector3 LeftShoulder.X, firstuser.LeftShoulder.Y), Color.DarkBlue);
        Vector3 LeftElbow.X, firstuser.LeftElbow.Y), Color.DarkBlue);
        Vector3 LeftHand.X, firstuser.LeftHand.Y), Color.DarkBlue);
        Vector2 RightShoulder.X, firstuser.RightShoulder.Y), Color.DarkBlue);
        Vector2 RightElbow.X, firstuser.RightElbow.Y), Color.DarkBlue);
        Vector2 RightHand.X, firstuser.RightHand.Y), Color.DarkBlue);
        Vector2 Torso.X, firstuser.Torso.Y), Color.DarkBlue);
        Vector2 LeftHip.X, firstuser.LeftHip.Y), Color.DarkBlue);
        Vector2 LeftKnee.X, firstuser.LeftKnee.Y), Color.DarkBlue);
        Vector2 LeftFoot.X, firstuser.LeftFoot.Y), Color.DarkBlue);
        Vector2 RightHip.X, firstuser.RightHip.Y), Color.DarkBlue);
        Vector2 RightKnee.X, firstuser.RightKnee.Y), Color.DarkBlue);
        Vector2 RightFoot.*/

        public Avatar()
        {
            Head = new GameObject();
            Neck = new GameObject();
            LeftShoulder = new GameObject();
            LeftElbow = new GameObject();
            LeftHand = new GameObject();
            RightShoulder = new GameObject();
            RightElbow = new GameObject();
            RightHand = new GameObject();
            Torso = new GameObject();
            LeftHip = new GameObject();
            LeftKnee = new GameObject();
            LeftFoot = new GameObject();
            RightHip = new GameObject();
            RightKnee = new GameObject();
            RightFoot = new GameObject();

            Vector3 tStartPosition = new Vector3(0, 0, -40.0f);
            Head.position = tStartPosition;
            Neck.position = tStartPosition;
            LeftShoulder.position = tStartPosition;
            LeftElbow.position = tStartPosition;
            LeftHand.position = tStartPosition;
            RightShoulder.position = tStartPosition;
            RightElbow.position = tStartPosition;
            RightHand.position = tStartPosition;
            Torso.position = tStartPosition;
            LeftHip.position = tStartPosition;
            LeftKnee.position = tStartPosition;
            LeftFoot.position = tStartPosition;
            RightHip.position = tStartPosition;
            RightKnee.position = tStartPosition;
            RightFoot.position = tStartPosition;
        }

        public void loadModel(GraphicsDevice graphicsDevice, Model pModel)
        {
            Head.loadModel(pModel);
            Neck.loadModel(pModel);
            LeftShoulder.loadModel(pModel);
            LeftElbow.loadModel(pModel);
            LeftHand.loadModel(pModel);
            RightShoulder.loadModel(pModel);
            RightElbow.loadModel(pModel);
            RightHand.loadModel(pModel);
            Torso.loadModel(pModel);
            LeftHip.loadModel(pModel);
            LeftKnee.loadModel(pModel);
            LeftFoot.loadModel(pModel);
            RightHip.loadModel(pModel);
            RightKnee.loadModel(pModel);
            RightFoot.loadModel(pModel);

        }

        public void Update()
        {
            /*
            Head;
            Neck;
            LeftShoulder;
             LeftElbow;
             LeftHand;
             RightShoulder;
             RightElbow;
             RightHand;
             Torso;
             LeftHip;
             LeftKnee;
             LeftFoot;
             RightHip;
             RightKnee;
             RightFoot;*/
        }

        public void DrawLine(Vector2 p1, Vector2 p2, Color color, ScreenManager screenManager)
        {
            pixel_tex = new Texture2D(screenManager.GraphicsDevice, KinectMusicShooterGame.width, KinectMusicShooterGame.height);
            SpriteBatch spriteBatch = screenManager.SpriteBatch;
            float distance = Vector2.Distance(p1, p2);

            float angle = (float)Math.Atan2((double)(p2.Y - p1.Y), (double)(p2.X - p1.X));
            spriteBatch.Begin();
            spriteBatch.Draw(pixel_tex, p1, null, color, angle, Vector2.Zero, new Vector2(distance, 1), SpriteEffects.None, 1);
            spriteBatch.End();
        }

        public void getuser(NuiUser firstuser)
        {
            if (width == 0 && height == 0)
            {
                width = -(float)firstuser.Head.X;
                height = -(float)firstuser.Head.Y;
            }

            OldRightHand.Z = RightHand.position.Z;
            OldRightHand.Y = RightHand.position.Y;
            OldRightHand.X = RightHand.position.X;

            OldLeftHand.Z = LeftHand.position.Z; //2.Hand Z
            OldLeftHand.Y = LeftHand.position.Y; //2.Hand Y
            OldLeftHand.X = LeftHand.position.X; //2.Hand X


            updateParts(firstuser.Head.X, firstuser.Head.Y, firstuser.Head.Z, Head);
            updateParts(firstuser.Neck.X, firstuser.Neck.Y, firstuser.Neck.Z, Neck);
            updateParts(firstuser.LeftShoulder.X, firstuser.LeftShoulder.Y, firstuser.LeftShoulder.Z, LeftShoulder);
            updateParts(firstuser.LeftElbow.X, firstuser.LeftElbow.Y, firstuser.LeftElbow.Z, LeftElbow);
            updateParts(firstuser.LeftHand.X, firstuser.LeftHand.Y, firstuser.LeftHand.Z, LeftHand);
            updateParts(firstuser.RightShoulder.X, firstuser.RightShoulder.Y, firstuser.RightShoulder.Z, RightShoulder);

            updateParts(firstuser.RightElbow.X, firstuser.RightElbow.Y, firstuser.RightElbow.Z, RightElbow);
            updateParts(firstuser.RightHand.X, firstuser.RightHand.Y, firstuser.RightHand.Z, RightHand);
            updateParts(firstuser.Torso.X, firstuser.Torso.Y, firstuser.Torso.Z, Torso);
            updateParts(firstuser.LeftHip.X, firstuser.LeftHip.Y, firstuser.LeftHip.Z, LeftHip);
            updateParts(firstuser.LeftKnee.X, firstuser.LeftKnee.Y, firstuser.LeftKnee.Z, LeftKnee);
            updateParts(firstuser.LeftFoot.X, firstuser.LeftFoot.Y, firstuser.LeftFoot.Z, LeftFoot);
            updateParts(firstuser.RightHip.X, firstuser.RightHip.Y, firstuser.RightHip.Z, RightHip);
            updateParts(firstuser.RightKnee.X, firstuser.RightKnee.Y, firstuser.RightKnee.Z, RightKnee);
            updateParts(firstuser.RightFoot.X, firstuser.RightFoot.Y, firstuser.RightFoot.Z, RightFoot);

            /*spriteBatch.DrawString(gameFont, "O", new Vector2(firstuser.LeftShoulder.X, firstuser.LeftShoulder.Y), Color.DarkBlue);
            spriteBatch.DrawString(gameFont, "O", new Vector2(firstuser.LeftElbow.X, firstuser.LeftElbow.Y), Color.DarkBlue);
            spriteBatch.DrawString(gameFont, "O", new Vector2(firstuser.LeftHand.X, firstuser.LeftHand.Y), Color.DarkBlue);
            spriteBatch.DrawString(gameFont, "O", new Vector2(firstuser.RightShoulder.X, firstuser.RightShoulder.Y), Color.DarkBlue);
            spriteBatch.DrawString(gameFont, "O", new Vector2(firstuser.RightElbow.X, firstuser.RightElbow.Y), Color.DarkBlue);
            spriteBatch.DrawString(gameFont, "O", new Vector2(firstuser.RightHand.X, firstuser.RightHand.Y), Color.DarkBlue);
            spriteBatch.DrawString(gameFont, "O", new Vector2(firstuser.Torso.X, firstuser.Torso.Y), Color.DarkBlue);
            spriteBatch.DrawString(gameFont, "O", new Vector2(firstuser.LeftHip.X, firstuser.LeftHip.Y), Color.DarkBlue);
            spriteBatch.DrawString(gameFont, "O", new Vector2(firstuser.LeftKnee.X, firstuser.LeftKnee.Y), Color.DarkBlue);
            spriteBatch.DrawString(gameFont, "O", new Vector2(firstuser.LeftFoot.X, firstuser.LeftFoot.Y), Color.DarkBlue);
            spriteBatch.DrawString(gameFont, "O", new Vector2(firstuser.RightHip.X, firstuser.RightHip.Y), Color.DarkBlue);
            spriteBatch.DrawString(gameFont, "O", new Vector2(firstuser.RightKnee.X, firstuser.RightKnee.Y), Color.DarkBlue);
            spriteBatch.DrawString(gameFont, "O", new Vector2(firstuser.RightFoot.X, firstuser.RightFoot.Y), Color.DarkBlue);
            spriteBatch.End();*/
        }

        public bool wurf()
        {
          
            if ((RightHand.position - OldRightHand).Z >= 0.3f && (RightHand.position - OldRightHand).Length() >= 0.7f)
            {
                if (WurfNullright == true)
                {
                    WurfEnd = RightHand.position;
                }
                else
                {
                    WurfStart = RightHand.position;
                    WurfNullright = true;
                }
            }
            else if (WurfNullright == true)
            {
                return true;
            }
            return false;
        }

        public bool wurfzwei()
        {
            if ((LeftHand.position - OldLeftHand).Z >= 0.3f && (LeftHand.position - OldLeftHand).Length() >= 0.7f)
            {
                if (WurfNullleft == true)
                {
                    WurfEndLeft = LeftHand.position;
                    
                }
                else
                {
                    WurfStartLeft = LeftHand.position;
                    WurfNullleft = true;
                }
                                
            }
            else if (WurfNullleft == true)
            {
                return true;
            }
            return false;
        }


        public bool hadoken()
        {
            float sensib = 3.0f;
            if ((float)(RightHand.position - LeftHand.position).Length() <= sensib)
            {
                HadokenReady = true;
                /*if ((float)(LeftHip.position - RightHand.position).Z <= 0.3f && HadokenReady == false)
                {
                    HadokenReady = true;
                }*/
            }

            /*
            if ((RightHand.position - LeftHand.position).Length() <= sensib && HadokenReady == true)
            {
                float test = (float)(LeftHip.position - LeftHand.position).Length();
                test += 0;
                
                if ((float)(LeftHip.position - LeftHand.position).Length() >= 5.0f)
                {

                    HadokenActive = true;
                }
                
            }*/

            if ((RightHand.position - LeftHand.position).Length() >= sensib || HadokenCount > 100)
            {
                HadokenReady = false;
                HadokenActive = false;
            }
            else
            {
                HadokenCount += 1;
            }


            if (HadokenReady == true)
            {
                return true;
            }

            else
            {
                return false;
            }
            
        }


        public Vector3 HadokenDirection()
        {
            return (LeftHand.position - Torso.position);
        }


        public float vect()
        {
            return (RightHand.position-OldRightHand).Length();
        }

        public Vector3 getWurfVector()
        {
            WurfNullright = false;
            return (WurfEnd - WurfStart);

        }

        public Vector3 getWurfVectorLeft()
        {
            WurfNullleft = false;
            return (WurfEndLeft - WurfStartLeft);
        }

        private void updateParts(float x, float y, float z, GameObject Part)
        {
            Part.position.X = x + width;
            Part.position.Y = y + height;
            Part.position.Z = z;

            Part.position.Z *= -0.01f;
            Part.position.Z += 40.0f;
            Part.position.X /= -Part.position.Z;
            Part.position.Y /= -Part.position.Z;
        }

        public void Draw(GraphicsDevice graphicsDevice, Camera camera)
        {
            Head.Draw(graphicsDevice, camera);
            Neck.Draw(graphicsDevice, camera);
            LeftShoulder.Draw(graphicsDevice, camera);
            LeftElbow.Draw(graphicsDevice, camera);
            LeftHand.Draw(graphicsDevice, camera);
            RightShoulder.Draw(graphicsDevice, camera);
            RightElbow.Draw(graphicsDevice, camera);
            RightHand.Draw(graphicsDevice, camera);
            Torso.Draw(graphicsDevice, camera);
            LeftHip.Draw(graphicsDevice, camera);
            LeftKnee.Draw(graphicsDevice, camera);
            LeftFoot.Draw(graphicsDevice, camera);
            RightHip.Draw(graphicsDevice, camera);
            RightKnee.Draw(graphicsDevice, camera);
            RightFoot.Draw(graphicsDevice, camera);
        }
    }
}
